/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.wasabeef.example.glide.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.TableLayout;
import ohos.agp.components.ComponentContainer.LayoutConfig;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.LogUtil;
import com.bumptech.glide.request.RequestOptions;

import jp.wasabeef.glide.transformations.*;

import jp.wasabeef.example.glide.ResourceTable;

/**
 * TransformationsAbilitySlice
 */
public class TransformationsAbilitySlice extends AbilitySlice {
    private static final int ID_BUTTON = 200;
    private static final int ID_BUTTON2 = 300;
    private static final int ID_BUTTON3 = 400;
    private static final int ID_BUTTON4 = 500;
    private static final int ID_BUTTON5 = 600;
    private static final int ID_BUTTON6 = 700;
    private static final int ID_BUTTON7 = 800;
    private static final int ID_BUTTON8 = 900;
    private static final int ID_BUTTON9 = 1000;
    private static final int ID_BUTTON10 = 1000;
    private static final int ID_BUTTON11 = 1100;
    private static final int ID_BUTTON12 = 1200;
    private static final int ID_BUTTON13 = 1300;
    private static final int ID_BUTTON14 = 1400;
    private static final int ID_BUTTON15 = 1500;
    private static final int ID_BUTTON16 = 1600;
    private static final int ID_BUTTON17 = 1700;
    private static final int ID_BUTTON18 = 1800;
    private static String TAG = "GlideTransformations";
    /**
     * Image
     */
    public Image imageview;
    /**
     * Image
     */
    public Image imageview2;
    /**
     * Image
     */
    public Image imageview3;
    /**
     * Image
     */
    public Image imageView4;
    PixelMap myBitmap = null;
    DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_CONTENT);
    private ComponentContainer mRootId;
    private Context classcontext;
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;
    private Button button8;
    private TableLayout tablelayout;
    private DirectionalLayout vertiDirectionLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        classcontext = getContext();
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        tablelayout = initTable();
        button = initButton();
        tablelayout.addComponent(button);
        button2 = initButton2();
        tablelayout.addComponent(button2);
        button3 = initButton3();
        tablelayout.addComponent(button3);
        button4 = initButton4();
        tablelayout.addComponent(button4);
        button5 = initButton5();
        tablelayout.addComponent(button5);
        button6 = initButton6();
        tablelayout.addComponent(button6);
        button7 = initButton7();
        tablelayout.addComponent(button7);
        button8 = initButton8();
        tablelayout.addComponent(button8);
        DirectionalLayout directionalLayout = initDirectionalLayout();
        DirectionalLayout.LayoutConfig imglayoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        setImageview(directionalLayout, imglayoutConfig);
        setImageview2(directionalLayout, imglayoutConfig);
        setImageview3(directionalLayout, imglayoutConfig);
        setImageview4(directionalLayout, imglayoutConfig);
        super.setUIContent(directionalLayout);
    }

    private DirectionalLayout initDirectionalLayout() {
        vertiDirectionLayout = new DirectionalLayout(this);
        vertiDirectionLayout.setWidth(LayoutConfig.MATCH_PARENT);
        vertiDirectionLayout.setHeight(LayoutConfig.MATCH_PARENT);
        vertiDirectionLayout.setOrientation(Component.VERTICAL);
        vertiDirectionLayout.addComponent(tablelayout);
        return vertiDirectionLayout;
    }

    private void setImageview(DirectionalLayout vertiDirectionLayout, DirectionalLayout.LayoutConfig imglayoutConfig) {
        imageview = new Image(this);
        imageview.setLayoutConfig(imglayoutConfig);
        imageview.setHeight(600);
        imageview.setPadding(10, 1, 10, 1);
        vertiDirectionLayout.addComponent(imageview);
    }

    private void setImageview2(DirectionalLayout vertiDirectionLayout, DirectionalLayout.LayoutConfig imglayoutConfig) {
        imageview2 = new Image(this);
        imageview2.setLayoutConfig(imglayoutConfig);
        imageview2.setHeight(600);
        imageview2.setPadding(10, 1, 10, 1);
        vertiDirectionLayout.addComponent(imageview2);
    }

    private void setImageview3(DirectionalLayout vertiDirectionLayout, DirectionalLayout.LayoutConfig imglayoutConfig) {
        imageview3 = new Image(this);
        imageview3.setLayoutConfig(imglayoutConfig);
        imageview3.setHeight(600);
        imageview3.setPadding(10, 1, 10, 1);
        vertiDirectionLayout.addComponent(imageview3);
    }

    private void setImageview4(DirectionalLayout vertiDirectionLayout, DirectionalLayout.LayoutConfig imglayoutConfig) {
        imageView4 = new Image(this);
        imageView4.setLayoutConfig(imglayoutConfig);
        imageView4.setHeight(600);
        imageView4.setPadding(10, 1, 10, 1);
        vertiDirectionLayout.addComponent(imageView4);
    }

    private TableLayout initTable() {
        tablelayout = new TableLayout(this);
        tablelayout.setColumnCount(2);
        tablelayout.setRowCount(4);
        tablelayout.setWidth(LayoutConfig.MATCH_CONTENT);
        tablelayout.setHeight(LayoutConfig.MATCH_CONTENT);
        tablelayout.setOrientation(Component.HORIZONTAL);
        tablelayout.setMarginsLeftAndRight(20, 20);
        tablelayout.setMarginsTopAndBottom(20, 20);
        return tablelayout;
    }

    private Button initButton() {
        button = new Button(this);
        button.setLayoutConfig(layoutConfig);
        button.setText("Crop Square");
        button.setTextSize(50);
        button.setId(ID_BUTTON);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button.setBackground(background);
        button.setPadding(10, 10, 10, 10);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Glide1, loading CropSquareTransformation");
                Glide.with(classcontext)
                        .load(ResourceTable.Media_check)
                        .apply(RequestOptions.bitmapTransform(new CropSquareTransformation()))
                        .into(imageview);
            }
        });
        return button;
    }

    private Button initButton2() {
        button2 = new Button(this);
        button2.setLayoutConfig(layoutConfig);
        button2.setText("GrayScale");
        button2.setTextSize(50);
        button2.setId(ID_BUTTON2);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button2.setBackground(background);
        button2.setPadding(10, 10, 10, 10);
        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase2");
                // 10Feb21 : For images greater than 1024x768 only center part is getting grayScalled
                Glide.with(classcontext)
                        .load(ResourceTable.Media_check)
                        .apply(RequestOptions.bitmapTransform(new GrayscaleTransformation()))
                        .into(imageview);
            }
        });
        return button2;
    }

    private Button initButton3() {
        button3 = new Button(this);
        button3.setLayoutConfig(layoutConfig);
        button3.setText("ColorFilter");
        button3.setTextSize(50);
        button3.setId(ID_BUTTON3);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button3.setBackground(background);
        button3.setPadding(10, 10, 10, 10);
        button3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase3");
                // 10Feb21 : For images greater than 1024x768 only center part is getting colorFiltered
                Glide.with(classcontext)
                        .load(ResourceTable.Media_tiger)
                        .apply(RequestOptions.bitmapTransform(new ColorFilterTransformation(Color.argb(220, 255, 0, 10))))
                        .into(imageview);
            }
        });
        return button3;
    }

    private Button initButton4() {
        button4 = new Button(this);
        button4.setLayoutConfig(layoutConfig);
        button4.setText("BlurLight");
        button4.setTextSize(50);
        button4.setId(ID_BUTTON4);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button4.setBackground(background);
        button4.setPadding(10, 10, 10, 10);
        button4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase4");
                // 10Frb21 : Image size should be less than 1.5mb else giving OOM
                // radius value should be between 1 and 25. DeepBlur is not working
                Glide.with(classcontext)
                        .load(ResourceTable.Media_twitter)
                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(classcontext, 25)))
                        .into(imageview);
            }
        });
        return button4;
    }

    private Button initButton5() {
        button5 = new Button(this);
        button5.setLayoutConfig(layoutConfig);
        button5.setText("Crop Square internet");
        button5.setTextSize(50);
        button5.setId(ID_BUTTON5);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button5.setBackground(background);
        button5.setPadding(10, 10, 10, 10);
        button5.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase5");
                Glide.with(classcontext)
                        .load("https://i.imgur.com/DvpvklR.png")
                        .apply(RequestOptions.bitmapTransform(new CropSquareTransformation()))
                        .into(imageview);
            }
        });
        return button5;
    }

    private Button initButton6() {
        button6 = new Button(this);
        button6.setLayoutConfig(layoutConfig);
        button6.setText("Grayscale internet");
        button6.setTextSize(50);
        button6.setId(ID_BUTTON6);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button6.setBackground(background);
        button6.setPadding(10, 10, 10, 10);
        button6.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase6");
                // 10Feb21 : For images greater than 1024x768 only center part is getting grayScalled
                Glide.with(classcontext)
                        .load("https://i.imgur.com/DvpvklR.png")
                        .apply(RequestOptions.bitmapTransform(new GrayscaleTransformation()))
                        .into(imageview);
            }
        });
        return button6;
    }

    private Button initButton7() {
        button7 = new Button(this);
        button7.setLayoutConfig(layoutConfig);
        button7.setText("Colorfilter Internet");
        button7.setTextSize(50);
        button7.setId(ID_BUTTON7);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button7.setBackground(background);
        button7.setPadding(10, 10, 10, 10);
        button7.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase7");
                // 10Feb21 : For images greater than 1024x768 only center part is getting colorFiltered
                Glide.with(classcontext)
                        .load("https://picsum.photos/600")
                        .apply(RequestOptions.bitmapTransform(new ColorFilterTransformation(Color.argb(80, 255, 0, 10))))
                        .into(imageview);
            }
        });
        return button7;
    }

    private Button initButton8() {
        button8 = new Button(this);
        button8.setLayoutConfig(layoutConfig);
        button8.setText("BlurLight Internet");
        button8.setTextSize(50);
        button8.setId(ID_BUTTON8);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button8.setBackground(background);
        button8.setPadding(10, 10, 10, 10);
        button8.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Testcase8");
                // 10Frb21 : Image size should be less than 1.5mb else giving OOM
                // radius value should be between 1 and 25. DeepBlur is not working
                Glide.with(classcontext)
                        .load("https://picsum.photos/600")
                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(classcontext, 25)))
                        .into(imageview);
            }
        });
        LogUtil.info(TAG, "after ecuting initbutton8 before return");
        return button8;
    }
}